package n.info.activities;

import java.util.List;

import n.info.R;
import n.info.entities.FeedSite;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class FeedListActivity extends Activity {
   
	// ~ constants .................................................................................
	
	
	// ~ overriden methods .................................................................................
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed_list);
        
        showInfoSites() ;
    }


	// ~ private methods .................................................................................
    
    private void showInfoSites(){
    	ListView listView = (ListView) findViewById(R.id.listViewFeedList) ;
    	listView.setAdapter(new Adapter(this, R.id.imageViewFeedSite, FeedSite.getList()));
    }

	// ~ inner class ...........................................................................................
    
    private class Adapter extends ArrayAdapter<FeedSite>{
    	
    	LayoutInflater inflater ;
    	View.OnClickListener clickListener = new ClickListener() ;
    	
    	public Adapter(Context context, int viewResourceId, List<FeedSite> objects) {
	        super(context, viewResourceId, objects);
	        inflater = LayoutInflater.from(context);
        }

		@Override
    	public View getView (int position, View convertView, ViewGroup parent){
    		
			convertView = inflater.inflate(R.layout.feed_list_item, null);
    		ImageView img = (ImageView) convertView.findViewById(R.id.imageViewFeedSite);
    		
    		img.setTag(position);
    		FeedSite feedSite = this.getItem(position) ;
    		img.setImageResource(feedSite.getLogoID());
    		img.setOnClickListener(clickListener);
    		
    		return convertView ;
    	}
    }

    
    private class ClickListener implements View.OnClickListener{
		
		@Override
        public void onClick(View v) {
			
			Intent intent = new Intent(FeedListActivity.this, FeedReaderActivity.class);
	        intent.setAction(FeedReaderActivity.ACTION_FEED_READER);
	        intent.putExtra(FeedReaderActivity.EXTRA_FEED_SITE_POSITION, (Integer) v.getTag());
	       	        
	        startActivity(intent);
        }
		
	}
}